<?php
/**
* @version		$Id: controller.php 10869 2008-08-30 07:24:03Z willebil $
* @package		Joomla
* @subpackage	Surveys
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Static class to hold controller functions for the Survey component
 *
 * @static
 * @package		Joomla
 * @subpackage	Survey
 * @since		1.5
 */
class SurveyController extends JController
{
	/**
	 * Method to show the search view
	 *
	 * @access	public
	 * @since	1.5
	 */
	function display()
	{
		JRequest::setVar('view','survey'); // force it to be the surveys view
		parent::display();
	}

	/**
 	 * Add a vote to an option
 	 */
	function vote()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db			=& JFactory::getDBO();
		$survey_id	= JRequest::getVar( 'id', 0, '', 'int' );
		$part_id    = JRequest::getVar( 'part', 1, '', 'int' );
		$vote_id	= JRequest::getVar( 'voteid', 0, 'post', 'int' );
		$startId    = JRequest::getVar( 'startId', 0, 'post', 'int' );
		$endId      = JRequest::getVar( 'endId', 0, 'post', 'int' );
		
		$dataValue = array();
		for($i = $startId; $i <= $endId; $i++){
			$dataValue[$i] = JRequest::getVar( 'hd_value'.$i, null, 'post', 'string' );
			echo "<script> alert('".$dataValue[$i]."');</script>\n";
		}
		
		$survey =& JTable::getInstance('survey','Table');
		if (!$survey->load( $survey_id ) || $survey->published != 1) {
			JError::raiseWarning( 404, JText::_('ALERTNOTAUTH') );
			return;
		}

		require_once(JPATH_COMPONENT.DS.'models'.DS.'survey.php');
		$model = new SurveyModelSurvey();
		$vote_id = $model->vote( $survey_id, $vote_id, $startId, $endId, $dataValue);

		$msg = JText::_( 'Thanks for your vote!' );

		// set Itemid id for links
		$menu = &JSite::getMenu();
		$items	= $menu->getItems('link', 'index.php?option=com_survey&view=survey');

		$itemid = isset($items[0]) ? '&Itemid='.$items[0]->id : '';
		$voteid = '&voteid='.$vote_id;
		$partid = '&part='.$part_id;

		$this->setRedirect( JRoute::_('index.php?option=com_survey&id='. $survey_id.$partid.$voteid.$itemid, false), $msg );
	}
}
?>